//
//  CZStatusCell.swift
//  iOSWeibo10
//
//  Created by itcast on 16/5/15.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

// 微博cell,即能显示原创微博,也能显示转发微博
class CZStatusCell: UITableViewCell {
    
    /// 底部View的顶部约束
    private var bottomViewTopCons: Constraint?
    
    /// cell要显示内容需要模型, 控制器的tableView数据源给cell赋值模型
    var status: CZStatus? {
        didSet {
            // 2.cell给原创view设置模型
            originalView.status = status
            // cell给被转发的view赋值 被转发微博 模型
            
            bottomViewTopCons?.uninstall()
            
            if let retweet_status = status?.retweeted_status {
                retweetView.retweetStatus = retweet_status
                retweetView.hidden = false
                print("有被转发微博")
                
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            } else {
                retweetView.hidden = true
                print("没有被转发微博")
                
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    bottomViewTopCons = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 注册cell后系统创建cell的时候会调用这个构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareUI()
    }
    
    private func prepareUI() {
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        // 1.添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
        // 2.添加约束
        // 原创view
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(10)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            // 高度需要根据内容来确定,先测试就写死一个高度
//            let height = Int(arc4random_uniform(100)) + 50
//            make.height.equalTo(height)
        }
        
        // 被转发view
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
            // 测试,高度先写一个值
//            make.height.equalTo(50)
        }
        
        // bottomView参照retweetView的底部
        bottomView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
            make.trailing.equalTo(contentView)
            make.height.equalTo(37)
        }
        
        // 根据cell的约束自动计算行高,系统是去获取contentView的高度作为cell的高度
        contentView.snp_makeConstraints { (make) -> Void in
            // contentView
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.trailing.equalTo(self)
            
            // contentView的高度不要去参照cell,cell的高度本来就是来参照contentView的高度
            // contentView的底部约束需要参照子控件
            make.bottom.equalTo(bottomView)
        }
    }
    
    // MARK: - 懒加载
    /// 原创view
    private lazy var originalView: CZStatusOriginalView = CZStatusOriginalView()
    
    /// 被转发的view
    private lazy var retweetView: CZStatusRetweetView = CZStatusRetweetView()
    
    /// bottomView
    private lazy var bottomView: CZStatusBottomView = CZStatusBottomView()
}
